﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals
{
    public partial class ActualsBreakDown : System.Web.UI.Page
    {

        protected IActualBreakDownDashBoard _dashBoardBreakDown
        {
            get
            {
                if (ViewState["DashBoardBreakDown"] == null)
                {
                    IBusinessActualBreakDownDashBoard businessActualBreakDownDashBoard = new BusinessActualBreakDownDashBoard();
                    DateTime fromDate;
                    string[] _date = txtStartDate.Text.Split('-');

                    fromDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

                    DateTime toDate;
                    string[] _date1 = txtEndDate.Text.Split('-');

                    toDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));

                    IActualBreakDownDashBoard actualBreakDownDashBoard =
                        businessActualBreakDownDashBoard.GetActualBreakDownDashBoard(fromDate, toDate,
                                                                                     int.Parse(ddlProject.SelectedValue.ToString()),
                                                                                     int.Parse(ddlRelease.SelectedValue.ToString()));

                    ViewState["DashBoardBreakDown"] = actualBreakDownDashBoard;
                    return actualBreakDownDashBoard;

                }
                else
                {
                    return (IActualBreakDownDashBoard)ViewState["DashBoardBreakDown"];
                }

            }

        }

        protected IActualFiguersDashBoard _dashBoardActualFigures
        {
            get
            {
                if (ViewState["DashBoardActualFigures"] == null)
                {
                    IBusinessActualFiguresDashBoard  businessActualFiguresDashBoard  = new BusinessActualFiguresDashBoard();
                    DateTime fromDate;
                    string[] _date = txtStartDate.Text.Split('-');

                    fromDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

                    DateTime toDate;
                    string[] _date1 = txtEndDate.Text.Split('-');

                    toDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));

                    IActualFiguersDashBoard actualFiguersDashBoard =
                        businessActualFiguresDashBoard.GetActualFiguresDashBoard(fromDate, toDate,
                                                                                     int.Parse(ddlProject.SelectedValue.ToString()),
                                                                                     int.Parse(ddlRelease.SelectedValue.ToString()));

                    ViewState["DashBoardActualFigures"] = actualFiguersDashBoard;
                    return actualFiguersDashBoard;

                }
                else
                {
                    return (IActualFiguersDashBoard)ViewState["DashBoardActualFigures"];
                }

            }

        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    //IBusinessProject businessProject = new BusinessProject();
                    //IProject project = businessProject.GetProject(int.Parse(ddlProject.SelectedValue.ToString()));

                   
                    //lblProject.Text = project.ProjectDescription;
                    //lblRelease.Text = release.Description;

                    IBusinessProject businessproject = new BusinessProject();
                    List<IProject> projects = businessproject.GetProjects();
                    ddlProject.DataSource = projects;
                    ddlProject.DataTextField = "ProjectDescription";
                    ddlProject.DataValueField = "ProjectId";
                    ddlProject.DataBind();
                    ddlProject.Items.Insert(0, "Select");

                }
            }
        }

        protected void ddlProject_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }

        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }

        protected void ddlRelease_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IRelease release = businessRelease.GetRelease(int.Parse(ddlProject.SelectedValue.ToString()),
                                                          int.Parse(ddlRelease.SelectedValue.ToString()));
            txtStartDate.Text = release.StartDate.Date.ToString("MM-dd-yyyy");
            txtEndDate.Text = release.EndDate.Date.ToString("MM-dd-yyyy");

        }
        protected void ddlSearchType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSearchType.SelectedIndex == 1)
            {
                FillSprints();
                ddlSprint.Enabled = true;
            }
            else
            {
                ddlSprint.Enabled = false;
                if (ddlSearchType.SelectedIndex == 3)
                {
                    IBusinessRelease businessRelease = new BusinessRelease();
                    IRelease release = businessRelease.GetRelease(int.Parse(ddlProject.SelectedValue.ToString()),
                                                          int.Parse(ddlRelease.SelectedValue.ToString()));
                    txtStartDate.Text = release.StartDate.Date.ToString("MM-dd-yyyy");
                    txtEndDate.Text = release.EndDate.Date.ToString("MM-dd-yyyy");

                }
            }
        }

        protected void ddlSprint_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            IBusinessSprint businessSprint = new BusinessSprint();
            ISprint sprint = businessSprint.GetSprintWithRelease(int.Parse(ddlSprint.SelectedValue),
                                                      int.Parse(ddlRelease.SelectedValue.ToString()));
                                                      
            txtStartDate.Text = sprint.SprintStartDate.Date.ToString("MM-dd-yyyy");
            txtEndDate.Text = sprint.SprintEndDate.Date.ToString("MM-dd-yyyy");

        }

        protected void FillSprints()
        {
            IBusinessSprint businessSprint = new BusinessSprint();
            IList<ISprint> sprints = businessSprint.GetSprints(int.Parse(ddlProject.SelectedValue.ToString()),
                                                               int.Parse(ddlRelease.SelectedValue.ToString()));

            /*  IList<string> names = new List<string>();

              foreach (Item item in Items)
                  if (!names.Contains(item.Name))
                      names.Add(name);

              ddlName.DataSource = names;
              ddlName.DataBind();
             ddlName.DataSource = items.Select(item=>item.Name).Distinct().ToList();
  ddlName.DataBind();
             * */


            ddlSprint.DataSource = sprints;
            ddlSprint.DataValueField = "SprintId";
            ddlSprint.DataTextField = "SprintDescription";
            ddlSprint.DataBind();
            ddlSprint.Items.Insert(0, "Select");

        }

        protected void btnGetBreakDown_OnClick(object sender, EventArgs e)
        {
            ViewState["DashBoardBreakDown"] = null;
            ViewState["DashBoardActualFigures"] = null;
            BindGridViewForDevelopers(_dashBoardBreakDown.DevelopersBreakDown);
            BindGridViewForBa(_dashBoardBreakDown.BusinessAnalystsBreakDown);
            BindGridViewForActualFiguresFullTime(_dashBoardActualFigures.ActualFiguresForFullTimers);
            BindGridViewForActualFiguresPartTime(_dashBoardActualFigures.ActualFiguresForPartTimes);
            lblTotalActualFiguresValue.Text = _dashBoardActualFigures.TotalHours.ToString();
            IBusinessActual businessActual = new BusinessActual();

            DateTime fromDate;
            string[] _date = txtStartDate.Text.Split('-');

            fromDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

            DateTime toDate;
            string[] _date1 = txtEndDate.Text.Split('-');

            toDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));

            IList<IActualBreakDownByEmployee> breakDownByEmployees = businessActual.GetActualsGroupByDevelopers(fromDate, toDate,
                                                       int.Parse(ddlProject.SelectedValue.ToString()),
                                                       int.Parse(ddlRelease.SelectedValue.ToString()));

            BindGridViewActualsByEmp(breakDownByEmployees);
            
        }

        protected void BindGridViewForDevelopers(IList<IActualBreakDown> actualBreakDowns)
        {   
            gvBreakDown.DataSource = actualBreakDowns;
            gvBreakDown.DataBind();
        }

        protected void BindGridViewActualsByEmp(IList<IActualBreakDownByEmployee> actualBreakDowns)
        {
            gvActualsByEmp.DataSource = actualBreakDowns;
            gvActualsByEmp.DataBind();
        }

        protected void BindGridViewForBa(IList<IActualBreakDown> actualBreakDowns)
        {
            gvBaBreakDown.DataSource = actualBreakDowns;
            gvBaBreakDown.DataBind();
        }

        protected void BindGridViewForActualFiguresFullTime(IList<IActualFigures> actualFigureses)
        {
            gvActualFigures.DataSource = actualFigureses;
            gvActualFigures.DataBind();
        }
        
        protected void BindGridViewForActualFiguresPartTime(IList<IActualFiguresPartTime> actualFigureses)
        {
            gvActualFiguresPartTimers.DataSource = actualFigureses;
            gvActualFiguresPartTimers.DataBind();
        }

        protected void gvBreakDown_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                e.Row.Cells[1].Text = _dashBoardBreakDown.DevelopersTotal.ToString();

            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                e.Row.Attributes.Add("style", "cursor:pointer;");
                int taskId = (int)  DataBinder.Eval(e.Row.DataItem, "TaskId");
                string fromdate = txtStartDate.Text;
                string todate = txtEndDate.Text;
                e.Row.Attributes.Add("onclick", "location='ShowActuals.aspx?taskid=" + taskId + "&startdate=" + fromdate + "&enddate=" + todate + "'");
            }

        }

        protected void gvBaBreakDown_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                e.Row.Cells[1].Text = _dashBoardBreakDown.BusinessAnalystTotal.ToString();

            }
        }

        protected void gvActualFigures_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                e.Row.Cells[3].Text = _dashBoardActualFigures.TotalHoursForFullTimers.ToString();

            }
        }

        protected void gvActualFiguresPartTimers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                e.Row.Cells[3].Text = _dashBoardActualFigures.TotalHoursForPartTimers.ToString();

            }
        }

        
    }
}